<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a href="" :class="{clickColor:subjectOne === ''}" @click="clearSubject">全部</a>
                </li>
                <li v-for="(subjectParent, index) in subjectParentTitle" :key="index">
                  <a href="#" :class="{clickColor:subjectOne === index}" @click="searchOne(subjectParent.id, index)">{{subjectParent.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(subject, index) in subjectTitle" :key="index">
                  <a href="#" :class="{clickColor:subjectTwo === index}" @click="searchTwo(subject.id, index)">{{subject.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <el-button type="text" :class="{clickBackColor:status === 0}" @click="newCourse">最新</el-button>
              <el-button type="text" :class="{clickBackColor:status === 1}" @click="hotCourse">最热</el-button>
              <el-button type="text" :class="{clickBackColor:status === 2}" @click="freeCourse">免费课程</el-button>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="this.total === 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="course in courseList" :key="course.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="course.cover" class="img-responsive" alt="">
                    <div class="cc-mask">
                      <a :href="'/course/' + course.id" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="'/course/' + course.id" class="course-title fsize18 c-333">{{course.title}}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green" v-if="course === 0" >
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{course.buyCount}}</i>
                      |
                      <i class="c-999 f-fA">9634评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="page"
          :page-size="limit"
          @current-change="getPage"
          :total="this.total">
        </el-pagination>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import course from "@/api/course";
export default {
  data() {
    return {
      page: 1,
      limit: 8,
      total: 1,
      courseList: [],
      courseFrontVO: {},
      subjectParentTitle: [],
      subjectTitle: [],
      subjectOne: '',
      subjectTwo: '',
      orderBy: 0,
      status: 0
    }
  },
  created() {
    this.getPage(this.page)
    this.getAllSubject()
  },
  methods: {
    getPage(page = 1) {
      this.page = page
      if (this.courseFrontVO == undefined) {
        this.getCourseList()
      }else if (this.courseFrontVO.buyCountSort !== 'buy_count' || this.courseFrontVO.priceSort !== '0'){
        this.searchTwo(this.courseFrontVO.subjectId, this.subjectTwo)
      } else {
        this.orderByData(this.courseFrontVO, this.subjectTwo)
      }
    },
    // 查询所有课程信息
    getCourseList() {
      course.getCourseFrontList(this.page, this.limit)
        .then(res => {
          this.courseList = res.data.data.rows
          this.total = res.data.data.total
        })
    },
    // 获取全部分类
    getAllSubject() {
      course.getAllSubject()
        .then(res => {
          this.subjectParentTitle = res.data.data.list
        })
    },
    // 点击一级分类显示二级分类
    searchOne(subjectParent, index) {
      // 查询对应的二级分类
      for (let i = 0; i < this.subjectParentTitle.length; i++) {
        // 判断两个id是否相同
        let oneSubject = this.subjectParentTitle[i];
        if (subjectParent === oneSubject.id) {
          this.subjectTitle = oneSubject.children
          this.subjectOne = index
        }
      }
    },
    // 点击二级分类查询课程信息
    searchTwo(subjectId, index) {
      this.courseFrontVO.subjectId = subjectId
      course.getCourseFrontList(this.page, this.limit, this.courseFrontVO)
        .then(res => {
          this.subjectTwo = index;
          this.courseList = res.data.data.rows
          this.total = res.data.data.total
        })
    },
    // 排序方式
    orderByData (courseFrontVO, index) {
      console.log(courseFrontVO)
      course.getCourseFrontList(this.page, this.limit, this.courseFrontVO)
        .then(res => {
          this.subjectTwo = index;
          this.courseList = res.data.data.rows
          this.total = res.data.data.total
        })
    },
    // 时间排序
    newCourse() {
      this.status = 0
      this.courseFrontVO.buyCountSort = ''
      this.orderByData(this.courseFrontVO, this.subjectTwo)
    },
    // 热度排序
    hotCourse() {
      this.status = 1
      this.courseFrontVO.buyCountSort = 'buy_count'
      this.courseFrontVO.priceSort = ''
      this.orderByData(this.courseFrontVO, this.subjectTwo)
    },
    // 免费课程
    freeCourse() {
      this.status = 2
      this.courseFrontVO.priceSort = '0'
      this.courseFrontVO.buyCountSort = ''
      this.orderByData(this.courseFrontVO, this.subjectTwo)
    },
    // 清空数组数据
    clearSubject() {
      this.getCourseList()
    }
  }
};
</script>
<style scoped>
#aCoursesList >>> .el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #68cb9b;
}
#aCoursesList >>> .el-pagination{
  margin-left: 510px;
}
.clickColor {
  color: #67C23A;
}
#aCoursesList >>>.clickColor:hover{
  color: #67C23A;
}
.clickBackColor {
  padding: 8px;
  background-color: #67C23A;
}
.clickBackColor >>> span {
  color: #FFFFFF;
}
#aCoursesList >>> .el-button--text {
  color: #67C23A;
}
</style>

